package se.lth.eit.eit060.proj2.server;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class Logger {
	private String logname = "log.txt";
	private String divide = ";";
	private String end;
	public static char n = 10;
	private static Logger myLogger;

	public Logger() {
		end = "#" + n;
	}

	/**
	 * log writes down what happens in the server! Can be changed if needed.
	 * 
	 * @param user
	 *            The String user is the name of the user whom do said action.
	 * @param action
	 *            The String that describes the action the user tries to do.
	 * @param record
	 *            The record the user tries to reach.
	 * @param ip
	 *            The IP of the user whom tries to reach the records.
	 */

	public void log(String user, String action, String record, String ip) {
		Calendar gc = new GregorianCalendar();
		gc.setTimeInMillis(System.currentTimeMillis());
		try {
			FileWriter fstream = new FileWriter(logname, true);
			BufferedWriter out = new BufferedWriter(fstream);
			out.write(user + divide + action + divide
					+ gc.getTime().toString() + divide + record + divide + ip
					+ end);
			out.close();
		} catch (Exception e) {
			System.err.println("Error: " + e.getMessage());
		}
	}

	static Logger getLogger() {
		if (myLogger == null)
			myLogger = new Logger();
		return myLogger;
	}

}
